// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Title Card Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.propertyValue : object.displayOffset

private alias object.value0 : object.timer
private alias object.value1 : object.circlePos
private alias object.value2 : object.actNamePos
private alias object.value3 : object.zoneTextPos
private alias object.value4 : object.actNumPos

private alias 0 : TITLECARD_SETUP
private alias 1 : TITLECARD_DELAY
private alias 2 : TITLECARD_ENTERTITLECARD
private alias 3 : TITLECARD_SHOWTITLECARD
private alias 4 : TITLECARD_SHOWSTAGE
private alias 5 : TITLECARD_STARTSTAGE
private alias 6 : TITLECARD_EXITTITLECARD
private alias 7 : TITLECARD_CREDITSFADEIN
private alias 8 : TITLECARD_FADETOTITLE

// Track ID Aliases
private alias 0 : TRACK_STAGE

// Player Aliases
private alias object.up : player.up
private alias object.down : player.down
private alias object.left : player.left
private alias object.right : player.right
private alias object.jumpPress : player.jumpPress
private alias object.jumpHold : player.jumpHold
private alias object.controlMode : player.controlMode
private alias object.value1 : player.timer

// Priority Aliases
private alias 2 : PRIORITY_ALWAYS         // Object will always update

// ========================
// Events
// ========================

event ObjectMain
	// Hide touch controls
	switch object.state
	case TITLECARD_SETUP
		StopMusic()

		// Init values and such
		object.alpha = 0xFF
		object.state++
		break

	case TITLECARD_DELAY
		// Wait for half a second
		object.timer++
		stage.state = STAGE_RUNNING
		if object.timer == 10
			object.state++
			object.timer = 0
		end if	
		break

	case TITLECARD_ENTERTITLECARD
			object.state++
			object.animation = 0
		break

	case TITLECARD_SHOWTITLECARD
			object.timer++

		
			if object.timer == 10
				object.animation = 1
			end if	

			if object.timer == 50
				object.animation = 2
			end if				

			if object.timer == 100
				object.animation = 3
				object.state = TITLECARD_SHOWSTAGE
				object.timer = 0
			end if				
		break
	case TITLECARD_SHOWSTAGE
		if object.alpha > 0
			object.alpha -= 16
		else
			object.state++
		end if
		break		
	case TITLECARD_STARTSTAGE
		if object.timer < 32
			object.timer++
		else
			object.timer = 0
			stage.timeEnabled = true
			print("stage.timeEnabled is true!", false, true)
			object.animation = 0
			object.state++
		end if
		break

	case TITLECARD_EXITTITLECARD
		if object.timer < 145
			object.timer++
		else
			PlayMusic(TRACK_STAGE)
			object.timer = 0
			object[0].controlmode = 0
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
end event


event ObjectDraw
	if object.state < TITLECARD_CREDITSFADEIN
		SetActivePalette(0, 0, screen.ysize)
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, object.alpha)
	end if

	object.drawOrder = 5

	switch object.animation
	case 0
	break
	case 1	//3
		temp0 = screen.xcenter
		temp0 -= 8
		temp0 += object.circlePos
		temp0 += object.displayOffset
		DrawSpriteScreenXY(0, temp0, 110)
	break
	case 2	//2
		temp0 = screen.xcenter
		temp0 -= 8
		temp0 += object.circlePos
		temp0 += object.displayOffset
		DrawSpriteScreenXY(1, temp0, 110)
	break	
	case 3	//1
		temp0 = screen.xcenter
		temp0 -= 8
		temp0 += object.circlePos
		temp0 += object.displayOffset
		DrawSpriteScreenXY(2, temp0, 110)
	break		
	end switch


end event


event ObjectStartup
	LoadSpriteSheet("3D_Test/Funkin.gif")
	SetMusicTrack("ForeverDemo.ogg", 0, 0)
	// Title card frames

	SpriteFrame(0, 0, 14, 24, 1, 45)	// #0  - 3
	SpriteFrame(0, 0, 14, 24, 16, 45)	// #1  - 2
	SpriteFrame(2, 0, 7, 24, 31, 45)	// #2  - 1

	// Cycle through all objects and filter out title cards
	// (Pretend this is a foreach, this object was made before foreaches were brought into existence)
	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Funk Card]
			object[arrayPos0].priority = PRIORITY_ALWAYS
			object[arrayPos0].alpha = 0xFF
			object[arrayPos0].drawOrder = 6
			stage.state = STAGE_PAUSED
		end if
		arrayPos0++
	loop
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 239, 239) // "Trigger" - #0
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "displayOffset")
end event
